<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class UserController extends AdminController
{

    public function index(Content $content)
    {
        return $content
            ->header('用户')
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new User(), function (Grid $grid) {

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('name', '昵称');
            });

            $grid->quickSearch('name')->placeholder('昵称快捷搜索');

            $grid->showActions();
            $grid->disableRowSelector();
            $grid->disableViewButton();
            $grid->disableCreateButton();
            $grid->scrollbarX();
            $grid->fixColumns(1, -1);

            $grid->model()->orderByDesc('id');

            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('avatar')->image('', 100);
//            $grid->column('openid');
            $grid->column('is_anchor', '主播')->switch()->help('启用此状态，用户才能提交直播间链接');
            $grid->column('status')->switch();
            $grid->column('created_at');
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->field('id');
            $show->field('openid');
            $show->field('name');
            $show->field('avatar');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id');
            $form->text('openid')->readOnly();
            $form->text('name');
            $form->image('avatar');

            $form->switch('is_anchor', '主播');
            $form->switch('status');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
